package com.zh.book_borrow.controller;


import com.zh.book_borrow.common.PageResult;
import com.zh.book_borrow.common.Result;
import com.zh.book_borrow.service.BookService;
import com.zh.book_borrow.vo.BookVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/book")
@Slf4j
public class BookController {
    @Autowired
    private BookService bookService;

    /**
     * 分页查询图书
     *
     * @param pageNum
     * @param pageSize
     * @param borrower
     * @param bookName
     * @return
     */
    @GetMapping("/page")
    public Result<PageResult<BookVO>> pageBook(@RequestParam(defaultValue = "1") Integer pageNum,
                                               @RequestParam(defaultValue = "5") Integer pageSize,
                                               @RequestParam(required = false) String borrower,
                                               @RequestParam(required = false) String bookName,
                                               @RequestParam(required = false) Integer state) {
        log.info("分页查询pageNum:{},pageSize:{},borrower:{},bookName:{},state:{}", pageNum, pageSize, borrower, bookName,state);
        PageResult<BookVO> pageResult = bookService.page(pageNum, pageSize, borrower, bookName,state);
        return Result.ok(pageResult);
    }

    /**
     * 借阅图书
     */
    @PutMapping("/borrow")
    public Result borrowBook(@RequestParam Long bookId, @RequestParam Long userId) {
        Integer state = 1;
        log.info("借阅图书bookId:{},userId:{},state:{}", bookId, userId, state);
        bookService.updateState(bookId, userId, state);
        return Result.ok("借阅成功，请按时归还~");
    }

    @PutMapping("/return")
    public Result returnBook(@RequestParam Long bookId, @RequestParam Long userId){
        Integer state =0;
        log.info("归还图书bookId:{},userId:{},state:{}", bookId, userId, state);
        bookService.updateState(bookId, userId, state);
        return Result.ok("归还成功，好样的！");
    }
    // 借阅图书
    // 归还图书
}
